﻿@using AntDesign.Internal
@using Project.Constraints.UI.Form
@typeparam TData where TData : class, new()
<Form Model="@Options.Data" LabelAlign="@AntLabelAlignType.Right" LabelColSpan="@Options.LabelSpan" OnFinish=OnFinish @ref=form WrapperColSpan="@Options.WrapperSpan">
    @foreach (var row in Options.GetRows())
    {
        var colSpan = 24 / row.Length;
        var lblSpan = GetLabelSpan(colSpan);
        <GridRow>
            @foreach (var col in row)
            {
                <GridCol Span="colSpan">
                    <FormItem LabelColSpan="lblSpan" Label="@(Localizer[col.Label])">
                        <InputBuilder Data="Options.Data" UI="Options.UI" Reciver="Options" Column="col"></InputBuilder>
                    </FormItem>
                </GridCol>
            }
        </GridRow>
    }
</Form>
@code {
    [Parameter, NotNull] public FormOptions<TData>? Options { get; set; }
    [Inject, NotNull] IStringLocalizer<TData>? Localizer { get; set; }
    IForm? form;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            Options.Validate = form != null ? form.Validate : null;
            Options.Update = StateHasChanged;
        }
    }

    private int GetLabelSpan(int colSpan)
    {
        if (colSpan == 24)
            return 6;

        return Options.LabelSpan;
    }

    Task OnFinish(EditContext context)
    {
        // context.Validate();
        return Task.CompletedTask;
    }
}
